package com.simdo.g768.Bean;

import com.g768.Bean.JsonObject;
import com.g768.MessageControl.MessageController;
import com.simdo.g768.Bean.object.Heartbeat;
import com.simdo.g768.Bean.object.TraceConfig;
import com.simdo.g768.Util.AppLog;
import com.simdo.g768.Util.GsonUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class DeviceInfo {

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSoftVer() {
        return softVer;
    }

    public void setSoftVer(String softVer) {
        this.softVer = softVer;
    }

    public String getFpgaVer() {
        return fpgaVer;
    }

    public void setFpgaVer(String fpgaVer) {
        this.fpgaVer = fpgaVer;
    }

    public String getHwVer() {
        return hwVer;
    }

    public void setHwVer(String hwVer) {
        this.hwVer = hwVer;
    }

    public Heartbeat getHeartbeat() {
        return GsonUtil.getPerson(heartbeat.toString(), Heartbeat.class);
    }

    public void setHeartbeat(JsonObject heartbeat) {
        this.heartbeat = heartbeat;
    }

    public int getWorkState() {
        return workState;
    }

    public void setWorkState(int workState) {
        this.workState = workState;
    }

    public int getDoState(int cellId) {
        return doState[cellId];
    }

    public void setDoState(int cellId, int doState) {
        this.doState[cellId] = doState;
    }

    public int getRfState(int cellId) {
        return rfState[cellId];
    }

    public void setRfState(int cellId, int rfState) {
        this.rfState[cellId] = rfState;
    }

    public TraceConfig getConfig(int cell) {
        return configMap.get(cell);
    }

    public void setConfig(int cell, TraceConfig traceConfig) {
        this.configMap.put(cell, traceConfig);
    }

    private String softVer, fpgaVer, hwVer, license; // 基带板密匙、软件版本、逻辑版本、硬件版本
    private JsonObject heartbeat; // 心跳信息
    private JsonObject ftpInfo; // 心跳信息
    private int workState; // 当前工作状态
    // doState 0: 启动   1: 启动中   2: 业务中/可结束   3: 结束中
    private final int[] doState;
    /**
     * rfState
     * case 0: //空闲，关闭、且不可点击
     * case 1: //启动中，关闭、且不可点击
     * case 2: //关闭中，打开、且不可点击
     * case 3: //已启动，关闭、且可点击
     * case 4: //已启动，打开、且可点击
     * */
    private final int[] rfState;

    // 小区配置信息
    private final HashMap<Integer, TraceConfig> configMap;

    private String id;

    public DeviceInfo(String id, JsonObject heartbeat) {
        this.id = id;
        this.heartbeat = heartbeat;
        this.softVer = "";
        this.fpgaVer = "";
        this.hwVer = "";
        this.license = "";

        configMap = new HashMap<>();

        this.workState = GnbBean.State.IDLE;
        doState = new int[6];
        rfState = new int[6];

        for (int i = 0; i < 6; i++){
            configMap.put(i, GsonUtil.getPerson(MessageController.build().templateTraceConfigCell().toString(), TraceConfig.class));
        }
    }

    public double[] getVoltage() {
        double[] voltage_s = new double[4];
        if (heartbeat == null) return voltage_s;
        try {
            JSONObject dev_info = heartbeat.getJSONObject("dev_info");

            JSONArray voltage = dev_info.getJSONArray("voltage");
            voltage_s[0] = voltage.getInt(0) / 1000.0;
            voltage_s[1] = voltage.getInt(1) / 1000.0;
        } catch (JSONException e) {
            AppLog.D("getVoltage err = " + e.getMessage());
        }
        return voltage_s;
    }

    public String getLicense() {
        return license;
    }

    public void setLicense(String license) {
        this.license = license;
    }
    public JsonObject getFtpInfo() {
        return ftpInfo;
    }

    public void setFtpInfo(JsonObject ftpInfo) {
        this.ftpInfo = ftpInfo;
    }
}
